package com.visionblue.business.ship.di

import android.content.Context
import androidx.room.Room
import com.jakewharton.disklrucache.DiskLruCache
import com.visionblue.business.ship.model.repository.DeviceInfoRepository
import com.visionblue.business.ship.model.repository.FlowVolumeRepository
import com.visionblue.business.ship.model.repository.db.AppDatabase
import com.visionblue.business.ship.model.repository.db.Migration1_2
import com.visionblue.business.ship.model.repository.db.Migration2_3
import com.visionblue.business.ship.page.MainRepository
import com.visionblue.business.ship.page.MainViewModel
import com.visionblue.business.ship.page.applications.ApplicationsRepository
import com.visionblue.business.ship.page.collection.CollectionRepository
import com.visionblue.business.ship.page.collection.CollectionViewModel
import com.visionblue.business.ship.page.orders.OrderRepository
import com.visionblue.business.ship.page.orders.OrderViewModel
import com.visionblue.business.ship.page.outer.OuterRepository
import com.visionblue.business.ship.page.outer.OuterViewModel
import com.visionblue.business.ship.page.register.config.MenuConfigRepository
import com.visionblue.business.ship.page.register.config.MenuConfigViewModel
import com.visionblue.business.ship.page.register.register.RegisterRepository
import com.visionblue.business.ship.page.register.register.RegisterViewModel
import org.koin.android.ext.koin.androidApplication
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module

/**
 * @author: xlh
 * @date: 2020-04-10 2:09 PM
 * @desc:
 */
val viewModelModule = module {

    viewModel { CollectionViewModel(get(), get(), get(), get(), get(), get()) }
//    single { }
//    viewModel { RecordViewModel() }
//    viewModel { PanelViewModel() }
    viewModel { MainViewModel(get(), get(), get()) }
    viewModel {
        RegisterViewModel(
            get(),
            get()
        )
    }
    viewModel { OuterViewModel(get()) }
    viewModel { MenuConfigViewModel(get()) }
    viewModel { OrderViewModel(get()) }
}

val repositoryModule = module {
    single { CollectionRepository(get(), get(), get(), get(), get()) }
    single { MainRepository(get()) }
    single { ApplicationsRepository() }
    single { DiskLruCache.open(androidApplication().cacheDir, 1, 1, 10 * 1024 * 1024) }
    single {
        DeviceInfoRepository.getInstance(
            androidApplication().getSharedPreferences(
                "deviceInfo",
                Context.MODE_PRIVATE
            )
        )
    }
    single {
        FlowVolumeRepository.getInstance(
            androidApplication().getSharedPreferences(
                "flowVolumeInfo",
                Context.MODE_PRIVATE
            )
        )
    }
    single { RegisterRepository() }
    single { OuterRepository() }
    single { MenuConfigRepository(get()) }
    single { OrderRepository() }
}

val dbModule = module {
    single {
        Room.databaseBuilder(
            get(),
            AppDatabase::class.java,
            "app_db"
        )
            .addMigrations(Migration1_2(get()), Migration2_3)
            .build()
    }
    single { get<AppDatabase>().collectionRecordDao() }
    single { get<AppDatabase>().cachedOrderDao() }
    single { get<AppDatabase>().recordAndOrderDao() }
    single { get<AppDatabase>().measureMenuItemsDao() }
}

val appModule = listOf(viewModelModule, repositoryModule, dbModule)